/*
This file is a textobot App HTTP Capture plugin.
*/

// ensure our module is loaded
const libtextobot = Module.load('/Library/MobileSubstrate/DynamicLibraries/textobot.dylib');
function TB_api(name, rettype, argstype) {
    return new NativeFunction(libtextobot.getExportByName(name), rettype, argstype);
}

// pickup textobot APIs
const textobot = {
    TB_logpath      : TB_api('TB_logpath', 'pointer', []),
    TB_logbuff      : TB_api('TB_logbuff', 'pointer', []),
    TB_logflush     : TB_api('TB_logflush', 'int', []),
    TB_dialog       : TB_api('TB_dialog','int', ['pointer']),
    TB_system       : TB_api('TB_system','int', ['pointer']),
    TB_root_system  : TB_api('TB_root_system', 'int', ['pointer']),
    TB_window       : TB_api('TB_window', 'pointer', []),
    TB_touch        : TB_api('TB_touch', 'int', ['int', 'int']),
    TB_swipe        : TB_api('TB_swipe', 'int', ['int', 'int', 'int', 'int']),
    // API likes TB_*_new should call TB_sptr_free to free the heap memory
    // see free function bellow
    TB_appdir_new   : TB_api('TB_appdir_new', 'pointer', []),
    TB_docdir_new   : TB_api('TB_docdir_new', 'pointer', []),
    TB_tmpdir_new   : TB_api('TB_tmpdir_new', 'pointer', []),
    TB_urlget_new   : TB_api('TB_urlget_new', 'pointer', ['pointer']),
    TB_urlpost_new  : TB_api('TB_urlpost_new', 'pointer', ['pointer', 'pointer']),
    TB_sptr_free    : TB_api('TB_sptr_free', 'void', ['pointer']),
    // ...
    // see https://gitee.com/geekneo/Textobot/blob/master/Doc/api.md for more APIs
};

// put the string to textobot's native buffer
function puts(s) {
    // logbuff's size is 4096
    if (s.length >= 4096) {
        console.log('Ignoring long string ' + s.length);
        return
    }
    textobot.TB_logbuff().writeUtf8String(s);
    return textobot.TB_logbuff();
}

// wrap logger util
function logs(s) {
    puts(s);
    const ret = textobot.TB_logflush();
    if (ret != 0) {
        console.log(ret + ' : ' + s);
    }
}

// wrap free util
function free(ptr) {
    var sptr = puts(ptr.toString(16))
    textobot.TB_sptr_free(sptr);
}

// slow logger, usually for global log
console.log("The Httpeek plugin's log file is at " + 
    textobot.TB_logpath().readUtf8String());

// fast log, usually for hooker with better performance
logs('The Httpeek plugin is running ...');

// load our native httpeek library
if (1) {
    Module.load('/var/mobile/Documents/Textobot/com_hybrage_httpeek/httpeek.so');
}
